package com.ellricky.market.ui

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.clue.ClueAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.MyClueData
import com.ellricky.baselib.entity.new.MyClueList
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.ClueSelectActivity
import com.ellricky.baselib.ui.clue.CluePoolDetailActivity
import com.ellricky.baselib.ui.consutant.CtSelectActivity
import com.ellricky.baselib.util.*
import com.ellricky.market.R
import kotlinx.android.synthetic.main.market_fragment_clue_pool.*
import java.util.*

class MarketCluePoolFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<MyClueData> = ArrayList<MyClueData>()
    private var refresh = false
    private val clueAdapter by lazy {
        ClueAdapter()
    }

    companion object {
        fun newInstance(): MarketCluePoolFragment {
            val fragment = MarketCluePoolFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.market_fragment_clue_pool
    }

    override fun initUIView(p0: View?) {
        setRefreshListener()
        loadDataOnInit()
        tv_select.setOnClickListener {
            jump<ClueSelectActivity> {  }
        }
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap["action"] = "clueList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["school_id"] = ""
        paramMap["page"] = rvView.curPage.toString()
        paramMap["page_size"] = "40"
    }

    override fun setAdapter(rv: RecyclerView) {
        val adapter = clueAdapter
        adapter.setOnItemClickListener(this)
        rv.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        if (callback != null) HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewECallback<MyClueList>(mContext) {
            override fun onSuccess(data: MyClueList, msg: String?) {
                tvClueNumber.text = "当前线索总条数：${data.clue_num}条"
                dataSource = data.data_list
                rvView.onLoadFinish(data.data_list, refresh)
            }
        }
    }

    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) clueAdapter.refresh(dataSource)
        else clueAdapter.loadMore(dataSource)
    }

    ///////////////////////////////////////////////////////////////////////////
    //  子类可重写的方法
    ///////////////////////////////////////////////////////////////////////////
    private fun setRefreshListener() {
        rvView.init(this)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val id = clueAdapter[position].clue_id
        jump<CluePoolDetailActivity> {
            putExtra(BaseConstant.KEY_ID, id)
        }
    }
}